﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Model.Manager;
using System.Data;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class RoleController : BaseController
    {
        #region 添加/修改角色
        [Description("添加/修改角色")]
        [ViewPage]
        public ActionResult AddRole(int RoleId = 0)
        {
            RoleModel role = new RoleModel();
            if (RoleId != 0)
            {
                ViewBag.Title = "修改角色";
                RoleInfo roleInfo = RoleBiz.Instance.GetRoleInfoById(RoleId);
                role.roleId = RoleId;
                role.roleCode = roleInfo.roleCode;
                role.roleName = roleInfo.roleName;
                role.remark = roleInfo.remark;
            }
            else
                ViewBag.Title = "添加角色";
            return View(role);
        }

        [Description("添加/修改角色")]
        [HttpPost]
        public ActionResult AddRole(RoleModel roleReq)
        {
            int ret = 0;
            string strOutPut = string.Empty;
            ResponseMsg resMsg = new ResponseMsg();
            if (ModelState.IsValid)
            {
                if (roleReq.roleId == 0)
                {
                    //添加角色
                    ret = RoleBiz.Instance.AddRole(roleReq.roleCode, roleReq.roleName, roleReq.remark, out strOutPut);
                }
                else
                {
                    //修改角色
                    ret = RoleBiz.Instance.EditRole(roleReq.roleId, roleReq.roleCode, roleReq.roleName, roleReq.remark, out strOutPut);
                }

                if (ret <= 0)
                {
                    resMsg.IsSuccess = false;
                    resMsg.Message = strOutPut;
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }

                resMsg.Message = strOutPut;
                resMsg.Body = Url.Action("RoleList", "Role");
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = ModelState.Values.First().Errors[0].ErrorMessage;
            }

            return Json(resMsg);
        }
        #endregion

        #region 删除角色
        [Description("删除角色")]
        [HttpPost]
        public ActionResult DelRole(int RoleId = 0)
        {
            int ret = 0;
            string strOutPut = string.Empty;
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.Body = Url.Action("RoleList", "Role");
            if (RoleId != 0)
            {
                ret = RoleBiz.Instance.DelRole(RoleId, out strOutPut);
                if (ret <= 0)
                {
                    resMsg.IsSuccess = false;
                    resMsg.Message = strOutPut;
                    return Json(resMsg, JsonRequestBehavior.AllowGet);
                }

                resMsg.Message = strOutPut;
            }
            else
            {
                resMsg.IsSuccess = false;
                resMsg.Message = "角色不存在";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            return Json(resMsg);
        }
        #endregion

        #region 角色列表
        [Description("查询角色列表")]
        [ViewPage]
        public ActionResult RoleList(string roleCode = "", string roleName = "", int pageIndex = 1, int pageSize = 10)
        {
            var ds = RoleBiz.Instance.GetPageList(roleCode, roleName, pageIndex, pageSize);
            var allrows = ConvertHelper.ConvertTo<int>(ds.GetTable(0).Rows[0]["allrows"], 0);
            IList<RoleModel> rolelist = new ModelHandler<RoleModel>().FillModel(ds, 1);
            PagedList<RoleModel> pagedlist = null;
            if (rolelist != null && rolelist.Count != 0)
                pagedlist = new PagedList<RoleModel>(rolelist, pageIndex, pageSize, allrows);
            return View(pagedlist);
        }
        #endregion

        #region 角色checkbox列表
        [Description("角色checkbox列表")]
        [NoVerify]
        public PartialViewResult CheckBoxRoleList(ElementAttribute EleAttr)
        {
            ViewBag.EleAttr = EleAttr;
            DataTable dtrole = RoleBiz.Instance.GetAllRoleList();
            IList<RoleModel> list = new ModelHandler<RoleModel>().FillModel(dtrole);

            return PartialView(list);
        }
        #endregion

        #region 角色设置菜单项
        [Description("角色设置菜单项")]
        [ViewPage]
        public ActionResult SetMenu(int RoleId, string RoleName)
        {
            RoleModel role = new RoleModel();
            if (RoleId != 0)
            {
                role.roleId = RoleId;
                role.roleName = RoleName;

                DataTable dt = RoleBiz.Instance.GetRoleMenuList(RoleId);
                if (dt != null && dt.Rows.Count != 0)
                {
                    string str = string.Empty;
                    foreach (DataRow item in dt.Rows)
                    {
                        str += item["MenuID"].ToString() + ",";
                    }
                    str = str.Substring(0, str.Length - 1);
                    role.MenuIds = str;
                }
            }

            return View(role);
        }

        [Description("角色设置菜单项")]
        [HttpPost]
        public ActionResult SetMenu(string RoleId, string MenuIds)
        {
            ResponseMsg resMsg = new ResponseMsg();
            string strOutPut = string.Empty;
            int ret = 0;

            ret = RoleBiz.Instance.SetRoleMenu(ConvertHelper.ConvertTo<int>(RoleId), MenuIds);
            if (ret <= 0)
            {
                resMsg.IsSuccess = false;
                resMsg.Message = "设置菜单失败";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            else
            {
                resMsg.Message = "设置菜单成功";
                resMsg.Body = Url.Action("RoleList", "Role");
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 角色设置action功能
        [Description("角色设置action功能")]
        [ViewPage]
        public ActionResult SetAction(int RoleId, string RoleName)
        {
            RoleModel role = new RoleModel();
            if (RoleId != 0)
            {
                role.roleId = RoleId;
                role.roleName = RoleName;

                DataTable dt = RoleBiz.Instance.GetRoleActionList(RoleId);
                if (dt != null && dt.Rows.Count != 0)
                {
                    string str = string.Empty;
                    foreach (DataRow item in dt.Rows)
                    {
                        str += item["ActionID"].ToString() + ",";
                    }
                    str = str.Substring(0, str.Length - 1);
                    role.ActionIds = str;
                }
            }

            return View(role);
        }

        [Description("角色设置action功能")]
        [HttpPost]
        public ActionResult SetAction(string RoleId, string ActionIds)
        {
            ResponseMsg resMsg = new ResponseMsg();
            string strOutPut = string.Empty;
            int ret = 0;

            ret = RoleBiz.Instance.SetRoleAction(ConvertHelper.ConvertTo<int>(RoleId), ActionIds);
            if (ret <= 0)
            {
                resMsg.IsSuccess = false;
                resMsg.Message = "设置action功能失败";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            else
            {
                resMsg.Message = "设置action功能成功";
                resMsg.Body = Url.Action("RoleList", "Role");
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}